//
//  XYJTabBarViewController.m
//  徙游记-01
//
//  Created by hcios on 2017/6/22.
//  Copyright © 2017年 鹿林. All rights reserved.
//

#import "XYJTabBarViewController.h"

//子viewController
#import "XYJToolViewController.h"
#import "XYJTravelsViewController.h"
#import "XYJWriteViewController.h"
#import "XYJScenicViewController.h"
#import "XYJMineViewController.h"

#import "XYJMainTabBar.h"
#import "XYJMainNavViewController.h"

@interface XYJTabBarViewController ()<XYJMainTabBarDelegate>

@property (nonatomic, weak)XYJMainTabBar * mainTabBar;

@property (nonatomic, strong)XYJToolViewController * toolVC;

@property (nonatomic, strong)XYJTravelsViewController * travelsVC;

@property (nonatomic, strong)XYJScenicViewController * scenicVC;

@property (nonatomic, strong)XYJMineViewController * mineVC;

@end

@implementation XYJTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpMainTabBar];
    [self setUpAllControllers];
    self.tabBar.translucent = NO;
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
}

- (void)setUpMainTabBar{
    XYJMainTabBar *mainTabBar = [[XYJMainTabBar alloc] init];
    mainTabBar.frame = self.tabBar.bounds;
    mainTabBar.delegate = self;
    [self.tabBar addSubview:mainTabBar];
    _mainTabBar = mainTabBar;
}


- (void)setUpAllControllers{
    NSArray *titles = @[@"工具", @"游记", @"景点", @"我的"];
    NSArray *images = @[@"tabbar-news", @"tabbar-tweet", @"tabbar-discover", @"tabbar-me"];
    NSArray *selectedImages = @[@"tabbar-news-selected", @"tabbar-tweet-selected", @"tabbar-discover-selected", @"tabbar-me-selected"];
    
    XYJToolViewController * toolVC = [[XYJToolViewController alloc] init];
    self.toolVC = toolVC;
    
    XYJTravelsViewController * travelsVC = [[XYJTravelsViewController alloc] init];
    self.travelsVC = travelsVC;
    
    XYJScenicViewController * scenicVC = [[XYJScenicViewController alloc] init];
    self.scenicVC = scenicVC;
    
    XYJMineViewController * mineVC = [[XYJMineViewController alloc] init];
    self.mineVC = mineVC;
    
    NSArray *viewControllers = @[toolVC, travelsVC, scenicVC, mineVC];
    
    for (int i = 0; i < viewControllers.count; i++) {
        UIViewController *childVc = viewControllers[i];
        [self SetupChildVc:childVc title:titles[i] image:images[i] selectedImage:selectedImages[i]];
    }
}

- (void)SetupChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)imageName selectedImage:(NSString *)selectedImageName{
    XYJMainNavViewController *nav = [[XYJMainNavViewController alloc] initWithRootViewController:childVc];
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    childVc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageName];
    childVc.tabBarItem.title = title;
    [self.mainTabBar addTabBarButtonWithTabBarItem:childVc.tabBarItem];
    [self addChildViewController:nav];
}



#pragma mark --------------------mainTabBar delegate
- (void)tabBar:(XYJMainTabBar *)tabBar didSelectedButtonFrom:(long)fromBtnTag to:(long)toBtnTag{
    self.selectedIndex = toBtnTag;
}

- (void)tabBarClickWriteButton:(XYJMainTabBar *)tabBar{
    XYJWriteViewController *writeVc = [[XYJWriteViewController alloc] init];
    XYJMainNavViewController *nav = [[XYJMainNavViewController alloc] initWithRootViewController:writeVc];
    
    [self presentViewController:nav animated:YES completion:nil];
}


@end
